package com.api.oa.mapper;

import com.api.oa.model.entity.YQUser;
import com.api.oa.model.vo.YQContacts;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQUserMapper {

    @Insert("insert into  yq_user  (" +
        "phone," +
        "insCode," +
        "idCard," +
        "uname," +
        "passwd," +
        "status," +
        "userType," +
        "belongInsCode," +
        "createdAt" +
    ") " +
    "values " +
    "(" +
        "#{entity.phone}," +
        "#{entity.insCode}," +
        "#{entity.idCard}," +
        "#{entity.uname}," +
        "#{entity.passwd}," +
        "#{entity.status}," +
        "#{entity.userType}," +
        "#{entity.belongInsCode}," +
        "#{entity.createdAt}" +
    ")" )
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "id")
    Integer insert(@Param("entity") YQUser entity);



    @Update("update  yq_user  set " +
        "phone = #{entity.phone}," +
        "insCode = #{entity.insCode}," +
        "idCard = #{entity.idCard}," +
        "uname = #{entity.uname}," +
        "status = #{entity.status}," +
        "userType = #{entity.userType}," +
        "emUuid = #{entity.emUuid}," +
        "belongInsCode = #{entity.belongInsCode}" +
    " where userId = #{entity.userId}")
    Integer update(@Param("entity") YQUser  entity);


    @Delete("delete from yq_user where userId=#{id}")
    void delete(@Param("id") Long id);


    @Select("<script> select * from  yq_user <where> 1=1 " +
            " <if test='belongInsCode!=null and belongInsCode !=\"\"'> and belongInsCode=#{belongInsCode}</if> " +
            " <if test='account!=null and account !=\"\"'> and concat(ifnull(uname,''),ifnull(idCard,''),ifnull(phone,'')) like concat('%',#{account},'%')</if> " +
            " <if test='role!=null and role !=\"\"'> and userType=#{role} </if> </where> </script>")
    List<YQUser> list(@Param("belongInsCode") String inCode,@Param("role") String role,@Param("account") String account);


    @Select("select * from  yq_user  where userId = #{id} limit 1")
    YQUser findOne(@Param("id") Long id);


    @Select("select * from  yq_user  where insCode = #{id} limit 1")
    YQUser findOneByInsCode(@Param("id") String id);


    @Select("select * from  yq_user  where idCard = #{id} limit 1")
    YQUser findOneByIDCard(@Param("id") String id);


    @Select("select * from  yq_user  where phone = #{account} or insCode= #{account} or idCard=#{account}  limit 1")
    YQUser findOneByAccount(@Param("account") String account);

    @Select("select * from  yq_user  where phone = #{account} limit 1")
    YQUser findOneByPhone(@Param("account") String account);


    @Update("update  yq_user  set " +
            "idCard = #{idCard}" +
            " where userId = #{userId}")
    void updateIDcard(@Param("userId") Long userId,@Param("idCard") String idCard);


    @Update("update  yq_user  set " +
            "belongInsCode = #{belongInsCode}" +
            " where userId = #{userId}")
    void updateBelongInsCode(@Param("belongInsCode")String requestIns, @Param("userId") Long userId);


    @Select("<script> select * from  yq_user  where userType = #{role} <if test='insCode!=null'> and belongInsCode = #{insCode}</if> </script>")
    List<YQUser> assUsers(@Param("insCode") String insCode, @Param("role") Integer role);


    @Update("update  yq_user  set " +
            "passwd = #{passwd}" +
            " where userId = #{userId}")
    Integer updatePwd(@Param("userId") Long account, @Param("passwd") String passwd);


    @Update("update  yq_user  set " +
            "phone = #{entity.phone}," +
            "uname = #{entity.uname}," +
            "belongInsCode = #{entity.belongInsCode}" +
            " where userId = #{entity.userId}")
    Integer updateUser(@Param("entity") YQUser  entity);


    /**
     * 修改积分
     * @param score
     * @param userId
     * @return
     */
    @Update("update yq_user set score = #{score} where userId = #{userId}")
    Integer updateScore(@Param("score")Double score,@Param("userId")Long userId);




    @Select("<script>select * from v_yq_contacts where emUuid is not null" +
            "<if test='orgId!=null'> and orgPid = #{orgId}</if>" +
            "<if test='pid!=null'> and orgId = #{pid} or orgPid = #{pid}</if>" +
            "<if test='keyWord!=null and keyWord!=\"\"'> and concat(ifnull(uname,''),ifnull(insName,'')) like concat('%',#{keyWord},'%') </if>" +
            " </script>")
    List<YQContacts> contacts(@Param("orgId") Long id, @Param("insCode") String belongInsCode, @Param("pid") Long pid, @Param("keyWord") String keyWord);

    /**
     *
     * @return
     */
    @Select("select * from v_yq_contacts where emUuid is  null")
    List<YQContacts>  sync();


    @Update("update yq_user set  emUuid = #{emUuid} where userId = #{userId}")
    void updateEmuid(@Param("userId") String userId, @Param("emUuid") String imUser);



    @Select("select * from v_yq_contacts where userId=#{userId}")
    YQContacts findOrgId(@Param("userId")Long id);
}

